package com.base.myproject.client.busi.service;
import java.util.Date;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.model.BarDataProvider;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.ScaleProvider;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.axis.XAxis;
import com.extjs.gxt.charts.client.model.charts.BarChart;
import com.extjs.gxt.charts.client.model.charts.BarChart.BarStyle;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Label;

/**
 * 日收入柱状图 日收入柱状图，与昨日对比，与最高只对比
 * 
 * @author zgw 2009-10-23 下午02:47:17 javazgw@gmail.com
 */
public class QueryIncomeDay extends ContentPanel {

	BaseStore<JDBCModelData> store;

	SelectionListener<ButtonEvent> listener;
	Button daybefore;
	Button dayafter;
	Button querybutton ;
	DateField querydate;
	public QueryIncomeDay() {
		init();
	}

	void init() {
		this.setHeaderVisible(false);
		this.setWidth("1024px");
		this.setHeight("720px");
		TableLayout tl = new TableLayout(5);
		tl.setWidth("300");
		setLayout(tl);
		initlistener();
		initstore();
		initQueryParamPanel();
		initWidget();

	}

	/**
	 * 查询参数，通过querybutton 的点击，组织sql
	 */
	void initQueryParamPanel() {
		
		TableData td = new TableData();
		td.setColspan(1);
		// td.setWidth("2000");
		td.setMargin(100);
		td.setPadding(5);
		td.setRowspan(1);

		Label date_label = new Label("查询日期:");
		 querydate = new DateField();
		querydate.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-M-d"));
		 querybutton = new Button("查询");
		 daybefore = new Button("向前一天查询");
		 dayafter = new Button("向后一天查询");
		this.add(date_label, td);
		this.add(querydate, td);
		this.add(daybefore, td);
		this.add(dayafter, td);
		this.add(querybutton, td);
		querybutton.addSelectionListener(listener);
		daybefore.addSelectionListener(listener);
		dayafter.addSelectionListener(listener);
	}

	/**
	 * 组织store
	 */
	void initstore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		// type.addField("b2cgcode");
		type.addField("paidnow");
		// type.addField("orderdate");
		// type.addField("cname");
		type.addField("paidy");
		type.addField("paidavg");

		// type.addField("totle");

		StoreInfo info = new StoreInfo();

		// info.setKeyname(new String[] { "b2cgcode" });
		info.setTablename("paidday");
		info.setNeedAllRow("yes");
		// info.setSql("");
		store = new BaseStore<JDBCModelData>(type, info, false);

	}

	/**
	 * 组织界面
	 */
	void initWidget() {
		initChart();
	}

	/**
	 * 组织chart
	 */
	void initChart() {
		ChartModel model = new ChartModel("日销售收入对比柱状图",
				"font-size: 16px; font-family: Verdana; text-align: center;");
		model.setBackgroundColour("#fefefe");
		model.setLegend(new Legend(Position.TOP, true));
		model.setScaleProvider(ScaleProvider.ROUNDED_NEAREST_SCALE_PROVIDER);

		 XAxis xa = new XAxis();  
//		 com.extjs.gxt.charts.client.model.axis.Label l = new com.extjs.gxt.charts.client.model.axis.Label("前日销售收入", 45);  
//		         l.setSize(10);  
//		         l.setColour("#000000");  
//		         xa.addLabels(l);  
//		 xa.setGridColour("-1");  
		 //   xa.addLabels("前日销售收入w");
		  //  xa.addLabels("当日销售收入"); 
		    xa.setLabels("John", "Frank", "Mary");  
//		    xa.addLabels("前10天的平均销售收入"); 
		 model.setXAxis(xa);  
		BarChart paidybar = new BarChart(BarStyle.GLASS);
		paidybar.setColour("#ff6600");
		BarDataProvider paidybarProvider = new BarDataProvider("paidy","John");
		paidybarProvider.bind(store);
		paidybar.setDataProvider(paidybarProvider);
		paidybar.setText("前日销售收入");
		paidybar.setTooltip("前日销售收入	#val#");
		//paidybar.setAnimateOnShow(true);
		// bar.addChartListener(listener);
		
	
		model.addChartConfig(paidybar);
		
		
		BarChart bar = new BarChart(BarStyle.GLASS);


		bar.setColour("#00aa00");
		BarDataProvider barProvider = new BarDataProvider("paidnow");
		barProvider.bind(store);
		bar.setDataProvider(barProvider);
		// bar.addChartListener(listener);
		bar.setText("当日销售收入");
		bar.setTooltip("当日销售收入		#val#");
		model.addChartConfig(bar);
		
		BarChart paidavgbar = new BarChart(BarStyle.GLASS);
		paidavgbar.setColour("#f11100");
		BarDataProvider paidavgbarProvider = new BarDataProvider("paidavg");
		paidavgbarProvider.bind(store);
		paidavgbar.setDataProvider(paidavgbarProvider);
		paidavgbar.setText("前10天的平均销售收入");
		paidavgbar.setTooltip("前10天的平均销售收入	#val#");
		// bar.addChartListener(listener);
		model.addChartConfig(paidavgbar);

//
//		LineChart line = new LineChart();
//		line.setAnimateOnShow(true);
//		line.setText("购物次数");
//		line.setColour("#00aa00");
//		LineDataProvider lineProvider = new LineDataProvider("listnum", "ym");
//		lineProvider.bind(store);
//		line.setDataProvider(lineProvider);
//		model.addChartConfig(line);
//
//		LineChart loginline = new LineChart();
//		loginline.setAnimateOnShow(true);
//		loginline.setText("登录次数");
//		loginline.setColour("#ff6600");
//		LineDataProvider loginlinelineProvider = new LineDataProvider(
//				"loginnum", "ym");
//		loginlinelineProvider.bind(store);
//		loginline.setDataProvider(loginlinelineProvider);
//		model.addChartConfig(loginline);
		String url = "chart/open-flash-chart.swf";
		final Chart chart = new Chart(url);
		// chart.setToolTip("点击产品的图型看看");
		chart.setChartModel(model);
		chart.setHeight("300px");
		chart.setWidth("768");
		
		TableData td = new TableData();
		td.setColspan(5);
		// td.setWidth("2000");
		td.setMargin(100);
		td.setPadding(5);
		td.setRowspan(1);
		add(chart,td);
		 
	}

	/**
	 * 查询按钮的事件
	 */
	void initlistener() {

		listener = new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				if (ce.getButton().getText().equals("查询")) {
					load();
				}
				else if(ce.getSource().equals(daybefore))
				{
					Date d = querydate.getValue();
					if(d==null)
						d = new Date();
	
					d.setDate(d.getDate()-1);
					querydate.setValue(d);
					load();
				}
				else if(ce.getSource().equals(dayafter))
				{
					Date d = querydate.getValue();
					if(d==null)
						d = new Date();
	
					d.setDate(d.getDate()+1);
					querydate.setValue(d);
					 load();
				}
			}
		};

	}
	public void load()
	{
		DateTimeFormat activeFormat = DateTimeFormat
		.getFormat("yyyy-M-d");
		
		
		String date = activeFormat.format(querydate.getValue());
		StoreInfo info = store.getStoreinfo();
		String str = "";
		str += "select a.paid as paidnow,b.paid as paidy,c.paid as paidavg from  ";
		str += "( ";
		str += "select sum(isnull(paid,0)) as paid,1 as i from s_service_po where datediff(day,servicestartdate ,'"+date+"')=0 and busistatus ='524288' ";
		str += ") as a ";

		str += "left join ";
		str += "( ";
		str += "select sum(isnull(paid,0)) as paid,1 as i from s_service_po where datediff(day,servicestartdate ,dateadd(day,-1,'"+date+"'))=0 and busistatus ='524288' ";
		str += ") as b ";
		str += "on a.i=b.i ";
		
		str += "left join ";
		str += "( ";
		str += "select avg(isnull(paid,0)) as paid,1 as i from s_service_po where datediff(day,servicestartdate ,'"+date+"')>0 and datediff(day,servicestartdate ,'"+date+"') <=10 and busistatus ='524288' ";
		str += ") as c ";
		str += "on a.i=c.i ";
		
		
		info.setSql(str);
		store.reload();

	}
}